<?php
/**
 * Timfan design模块处理程序
 *
 * @author Tim Fan
 * QQ:1026073477
 * @url http://i-fanr.com/
 */
defined('IN_IA') or exit('Access Denied');
define("TIM_QUIZ", "tim_quiz");
require_once IA_ROOT . "/addons/" . TIM_QUIZ . "/jssdk.class.php";
require_once IA_ROOT . "/addons/" . TIM_QUIZ . "/timUtil.class.php";
class Tim_quizModuleSite extends WeModuleSite {

	public function doMobileIndex() {
		global $_W,$_GPC;
		$openid = $_W['openid'];
		$uniacid = $_W['uniacid'];
		mc_fansinfo($openid);
		$user_name  = $_W['fans']['tag']['nickname'];
		$user_photo = $_W['fans']['tag']['avatar'];
		if (empty($_W['fans']['nickname'])) {
			mc_oauth_userinfo();
		}
		$openid = $_W['fans']['openid'];
		$user_name  = $_W['fans']['tag']['nickname'];
		$user_photo = $_W['fans']['tag']['avatar'];

		$date = date('Y-m-d');
		$nickname = $user_name;
		$member = pdo_fetch("SELECT * FROM ".tablename('tim_quiz_user')." WHERE `uniacid` = :uniacid and `answer_time` = :answer_time and `openid` = :openid", array(':uniacid' => $uniacid,':answer_time' => $date,':openid' => $openid));
		$score = intval($_GPC['score']);
		$tcent = intval($_GPC['tcent']);
		$lost_time = intval($_GPC['lost_time']);
		$hnum = intval($member['hnum']);
		$sets = pdo_fetch("SELECT * FROM ".tablename('tim_quiz_setting')." WHERE uniacid=$uniacid");
		$pd = intval($member['hnum']/$sets['tnum']);
		if(!empty($member)){
			if(intval($sets['dtnum']) >= $pd) {
				$data1 = array(
					"score" => $score,
					"lost_time" => $lost_time,
					"tcent" => $tcent,
					"hnum" => $hnum+1
				);
				pdo_update('tim_quiz_user', $data1, array('uniacid' => $uniacid,'answer_time' => $date,'openid' => $openid));
			} else { 
				echo "<script>alert('最多只能答题两次哦！');window.location.href='".$sets['text_url']."';</script>";
				return false;
			}
		} else { 
			$data2 = array(
				"openid" => $openid,
				"uniacid" => $uniacid,
				"nickname" => $nickname,
				"headimgurl" => $user_photo,
				"score" => $score,
				"tcent" => $tcent,
				"lost_time" => $lost_time,
				"answer_time" => $date
			);
			pdo_insert('tim_quiz_user', $data2);//添加数据
		}
		
	}
	
	public function doMobileCover1() { 
		global $_W,$_GPC;
		$uniacid = $_W['uniacid'];
		$openid = $_W['openid'];
		mc_fansinfo($openid);
		$user_name  = $_W['fans']['tag']['nickname'];
		$user_photo = $_W['fans']['tag']['avatar'];
		if (empty($_W['fans']['nickname'])) {
			mc_oauth_userinfo();
		}
		$openid = $_W['fans']['openid'];
		$user_name  = $_W['fans']['tag']['nickname'];
		$user_photo = $_W['fans']['tag']['avatar'];

		$date = date('Y-m-d');
		$member = pdo_fetch("SELECT * FROM ".tablename('tim_quiz_user')." WHERE `uniacid` = :uniacid and `answer_time` = :answer_time and `openid` = :openid", array(':uniacid' => $uniacid,':answer_time' => $date,':openid' => $openid));
		$sets = pdo_fetch("SELECT * FROM ".tablename('tim_quiz_setting')." WHERE uniacid=$uniacid");
		$pd = intval($member['hnum']/$sets['tnum']);
		if($pd == intval($sets['dtnum'])) { 
			echo "<script>alert('您今天的答题次数已经用完！');window.location.href='".$sets['text_url']."';</script>";
		} else { 
		$jnum = $sets['total'];	
		$pagenum = rand(1,$jnum);
		$total = $sets['tnum'];
		$list = pdo_fetchall("SELECT * FROM ".tablename('tim_quiz')." WHERE uniacid=$uniacid and pagenum=$pagenum ORDER BY qid ASC LIMIT 0,".($total-1));
		$all = pdo_fetchall("SELECT * FROM ".tablename('tim_quiz')." WHERE uniacid=$uniacid and pagenum=$pagenum ORDER BY qid ASC");
		$last_one = pdo_fetchall("SELECT * FROM ".tablename('tim_quiz')." WHERE uniacid=$uniacid and pagenum=$pagenum and qid=$total");
		$answer = array();
		$jifen = array();
		foreach ($all as $value) {
			$page_num = "page-".$value['qid'];
			$answer[$page_num] = $this->_zh($value['correct_ans']);
			$jifen[$page_num] = $value['cent'];
		}
		$result=array(
			array(
				"weixin1"=>$sets['score_scope11'],
				"weixin2"=>$sets['score_scope12'],
				"praise"=>$sets['praise1'],
			),
			array(
				"weixin1"=>$sets['score_scope21'],
				"weixin2"=>$sets['score_scope22'],
				"praise"=>$sets['praise2'],
			),
			array(
				"weixin1"=>$sets['score_scope31'],
				"weixin2"=>$sets['score_scope32'],
				"praise"=>$sets['praise3'],
			),
			array(
				"weixin1"=>$sets['score_scope41'],
				"weixin2"=>$sets['score_scope42'],
				"praise"=>$sets['praise4'],
			),
			array(
				"weixin1"=>$sets['score_scope51'],
				"weixin2"=>$sets['score_scope52'],
				"praise"=>$sets['praise5'],
			)
		);
		
		include $this->template('index');
		}
	}

	private function _zh($cans) { 
		switch ($cans)
		{
			case A:
			  return "0";
			  break;
			case B:
			  return "1";
			  break;
			case C:
			  return "2";
			  break;
			case D:
		      return "3";
			break;
			default:
			  return "Undefined";
		};
	}

	public function doWebTest() { 
		global $_W,$_GPC;
		$uniacid = $_W['uniacid'];
		$op = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
		load()->func('tpl');
		if('post' == $op){//添加或修改
			$id = intval($_GPC['id']);
			if(!empty($id)){
			$item = pdo_fetch("SELECT * FROM ".tablename('tim_quiz')." where id=$id and uniacid=$uniacid");
			empty($item)?message('亲,数据不存在！', '', 'error'):"";	
			}
			$info = pdo_fetch("SELECT * FROM ".tablename('tim_quiz_setting')." where uniacid=$uniacid");
 			
			if(checksubmit('submit')){
				empty ($_GPC['question'])?message('亲,问题不能为空'):$question=$_GPC['question'];
				$type =$_GPC['type'];
				$qid  =$_GPC['qid'];
				$pagenum  =$_GPC['pagenum'];
				$ans_a =$_GPC['ans_a'];
				$ans_b =$_GPC['ans_b'];
				$ans_c =$_GPC['ans_c'];
				$ans_d =$_GPC['ans_d'];
				$pic_a =$_GPC['pic_a'];
				$pic_b =$_GPC['pic_b'];
				$pic_c =$_GPC['pic_c'];
				$pic_d =$_GPC['pic_d'];
				$pic_a_intro =$_GPC['pic_a_intro'];
				$pic_b_intro =$_GPC['pic_b_intro'];
				$pic_c_intro =$_GPC['pic_c_intro'];
				$pic_d_intro =$_GPC['pic_d_intro'];
				$correct_ans =$_GPC['correct_ans'];
				$cent =$_GPC['cent'];
				$data = array(
					'id'=>$id,
					'uniacid' => $uniacid,
					'pagenum' => $pagenum,
					'question' =>$question,
					'type' =>$type,
					'qid'  =>$qid,
					'ans_a'=>$ans_a,
					'ans_b'=>$ans_b,
					'ans_c'=>$ans_c,
					'ans_d'=>$ans_d,
					'pic_a'=>$pic_a,
					'pic_b'=>$pic_b,
					'pic_c'=>$pic_c,
					'pic_d'=>$pic_d,
					'pic_a_intro'=>$pic_a_intro,
					'pic_b_intro'=>$pic_b_intro,
					'pic_c_intro'=>$pic_c_intro,
					'pic_d_intro'=>$pic_d_intro,
					'correct_ans'=>$correct_ans,
					'cent'=>$cent
				);
				
				if(empty($id)){
						pdo_insert('tim_quiz', $data);//添加数据
						message('数据添加成功！', $this->createWebUrl('test', array('op' => 'display')), 'success');
				}else{
						pdo_update('tim_quiz', $data, array('id' => $id));
						message('数据更新成功！', $this->createWebUrl('test', array('op' => 'display')), 'success');
				}
				
			}else{
				include $this->template('test');
			}
			
		}else if('del' == $op){//删除
		
			if(isset($_GPC['delete'])){
				$ids = implode(",",$_GPC['delete']);
				$sqls = "delete from  ".tablename('tim_quiz')."  where id in(".$ids.")"; 
				pdo_query($sqls);
				message('删除成功！', referer(), 'success');
			}
			$id = intval($_GPC['id']);
			$row = pdo_fetch("SELECT id FROM ".tablename('tim_quiz')." WHERE id = :id", array(':id' => $id));
			if (empty($row)) {
				//dump($_GPC);
				message('抱歉，数据不存在或是已经被删除！', $this->createWebUrl('test', array('op' => 'display')), 'error');
			}
			pdo_delete('tim_quiz', array('id' => $id));
			message('删除成功！', referer(), 'success');
			
		}else if('display' == $op){//显示
			$pindex = max(1, intval($_GPC['page']));
			$psize =20;//每页显示
			$condition = "WHERE uniacid= $uniacid";
			if (!empty($_GPC['keyword'])) {
				$condition .= " and  question LIKE '%".$_GPC['keyword']."%'  ";
			}
			
			$list = pdo_fetchall("SELECT *  FROM ".tablename('tim_quiz') ." $condition  ORDER BY id DESC LIMIT ".($pindex - 1) * $psize.','.$psize);//分页
			$total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('tim_quiz')." $condition" );
			$pager = pagination($total, $pindex, $psize);
			include $this->template('test');
		}
	}

	public function doWebParam() { 
		global $_W,$_GPC;
		$uniacid = $_W['uniacid'];
		load()->func('tpl');
		$info = pdo_fetch("SELECT * FROM ".tablename('tim_quiz_setting')." WHERE uniacid=$uniacid");
 			
		if(checksubmit('submit')){ 
				$title = $_GPC['title'];
				$copyright = $_GPC['copyright'];
				$total = $_GPC['total'];
				$tnum = $_GPC['tnum'];
				$dtnum = $_GPC['dtnum'];
				$duration = $_GPC['duration'];
				$score_scope11 = $_GPC['score_scope11'];
				$score_scope12 = $_GPC['score_scope12'];
				$praise1 = $_GPC['praise1'];
				$score_scope21 = $_GPC['score_scope21'];
				$score_scope22 = $_GPC['score_scope22'];
				$praise2 = $_GPC['praise2'];
				$score_scope31 = $_GPC['score_scope31'];
				$score_scope32 = $_GPC['score_scope32'];
				$praise3 = $_GPC['praise3'];
				$score_scope41 = $_GPC['score_scope41'];
				$score_scope42 = $_GPC['score_scope42'];
				$praise4 = $_GPC['praise4'];
				$score_scope51 = $_GPC['score_scope51'];
				$score_scope52 = $_GPC['score_scope52'];
				$praise5 = $_GPC['praise5'];
				$adv = $_GPC['adv'];
				$adv_link = $_GPC['adv_link'];
				$prize = $_GPC['prize'];
				$rule = $_GPC['rule'];
				$link = $_GPC['link'];
				$link1 = $_GPC['link1'];
				$link2 = $_GPC['link2'];
				$link3 = $_GPC['link3'];
				$link4 = $_GPC['link4'];
				$text_url = $_GPC['text_url'];
				$share_content = $_GPC['share_content'];
				$share_title = $_GPC['share_title'];
				$share_icon = $_GPC['share_icon'];
				$infos = array(
					'uniacid'=>$uniacid,
					'title' => $title, 
					'copyright' => $copyright,
					'total' => $total,
					'tnum' => $tnum,
					'dtnum' => $dtnum,
					'duration' => $duration,
					'score_scope11' => $score_scope11, 
					'score_scope12' => $score_scope12, 
					'praise1' => $praise1,
					'score_scope21' => $score_scope21, 
					'score_scope22' => $score_scope22, 
					'praise2' => $praise2,
					'score_scope31' => $score_scope31, 
					'score_scope32' => $score_scope32, 
					'praise3' => $praise3,
					'score_scope41' => $score_scope41, 
					'score_scope42' => $score_scope42, 
					'praise4' => $praise4,
					'score_scope51' => $score_scope51, 
					'score_scope52' => $score_scope52, 
					'praise5' => $praise5,
					'adv' => $adv,
					'adv_link' => $adv_link,
					'prize' => $prize,
					'rule' => $rule,
					'link' => $link,
					'link1' => $link1,
					'link2' => $link2,
					'link3' => $link3,
					'link4' => $link4,
					'text_url' => $text_url,
					'share_content' => $share_content, 
					'share_title' => $share_title, 
					'share_icon' => $share_icon
				);
			if(empty($info)){
					pdo_insert('tim_quiz_setting', $infos);//添加数据
					message('数据添加成功！', $this->createWebUrl('param'), 'success');
			}else{
					$id = $info['id'];
					pdo_update('tim_quiz_setting', $infos, array('id' => $id));
					message('数据更新成功！', $this->createWebUrl('param'), 'success');
			}	
		}
		include $this->template('param');
	}

	public function doWebUserinfo() {
		global $_W,$_GPC;
		$uniacid = $_W['uniacid'];
		load()->func('tpl');
		$op = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
		$pindex = max(1, intval($_GPC['page']));
		$psize =20;//每页显示
		$condition = "WHERE uniacid= $uniacid";
			if (!empty($_GPC['keyword'])) {
				$condition .= " and  question LIKE '%".$_GPC['keyword']."%'  ";
			}
			
		$uinfo = pdo_fetchall("SELECT *  FROM ".tablename('tim_quiz_user') ." $condition  ORDER BY id DESC LIMIT ".($pindex - 1) * $psize.','.$psize);//分页
		$total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('tim_quiz_user')." $condition" );
		$pager = pagination($total, $pindex, $psize);
		
		$set = pdo_fetch("SELECT * FROM ".tablename('tim_quiz_setting')." WHERE uniacid=$uniacid");

		if('del' == $op){
 		if(isset($_GPC['delete'])){
				$ids = implode(",",$_GPC['delete']);
				$sqls = "delete from  ".tablename('tim_quiz_user')."  where id in(".$ids.")"; 
				pdo_query($sqls);
				message('删除成功！', referer(), 'success');
			}
			$id = intval($_GPC['id']);
			$row = pdo_fetch("SELECT id FROM ".tablename('tim_quiz_user')." WHERE id = :id", array(':id' => $id));
			if (empty($row)) {
				//dump($_GPC);
				message('抱歉，数据不存在或是已经被删除！', $this->createWebUrl('userinfo'), 'error');
			}
			pdo_delete('tim_quiz_user', array('id' => $id));
			message('删除成功！', referer(), 'success');
		}	
		include $this->template('userinfo');
	}

	private function  getUserInfo ($access_token, $openid) 
	{
		load()->func('communication');

        $api_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" . $access_token . "&openid=" . $openid . "&lang=zh_CN";

        $content = ihttp_get($api_url);

        $userInfo = @json_decode($content['content'], true);

        return $userInfo;

	}

	public function doMobileShare() { 
		global $_W,$_GPC;
		$uniacid = $_W['uniacid'];
		$openid = $_W['openid'];
		mc_fansinfo($openid);
		$user_name  = $_W['fans']['tag']['nickname'];
		$user_photo = $_W['fans']['tag']['avatar'];
		if (empty($_W['fans']['nickname'])) {
			mc_oauth_userinfo();
		}
		$openid = $_W['fans']['openid'];
		$user_name  = $_W['fans']['tag']['nickname'];
		$user_photo = $_W['fans']['tag']['avatar'];
		$date = date('Y-m-d');
		$data1 = array(
			"isshare" => true,
		);
		pdo_update('tim_quiz_user', $data1, array('uniacid' => $uniacid,'answer_time' => $date,'openid' => $openid));
	}

	public function doMobileEgg() { 
		global $_W,$_GPC;
		$uniacid = $_W['uniacid'];
		$sets = pdo_fetch("SELECT * FROM ".tablename('tim_quiz_setting')." WHERE uniacid=$uniacid");
		include $this->template('egg');
	}

	public function doMobileChou() { 
		global $_W,$_GPC;
		$token = $_GPC['token'];
		if($token == "K*vaup*V3xDiZsCtTV/V7Go3gupe4fe1H6Y0fcZanmYm0sTUE1B8D2w2CA"){
			echo "{success:true}";
		}
	}
}